/*
* $Id: ChangeInspectView.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeInspectView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Visual/Drawers/EditorDrawer/SectionItem.h"
#include "Settings/Journal/RecordViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"
#include "Components/Journal/InspectManager.h"

#define TITLE_ID		1
#define DATE_INSPECT_ID			2
#define FRAME_INSPECT_ID		3
#define STRENGTH_INSPECT_ID		4
#define EBQ_INSPECT_ID			5
#define WAX_INSPECT_ID			6
#define AMBROSIA_INSPECT_ID		7
#define PUMP_INSPECT_ID			8
#define FEEDING_INSPECT_ID		9
#define DISEASES_INSPECT_ID		10
#define NOTE_INSPECT_ID			11
#define USER_FIELD_ID			12
#define HOUSING_FIELD_ID(i, j)	(i<<8)|j
#define HONEY_FIELD_ID			1
#define BROOD_FIELD_ID			2
#define OPEN_BROOD_FIELD_ID		3
#define CLOSE_BROOD_FIELD_ID	4
#define GOOGS_HONEY_FIELD_ID	5

#pragma warning(disable: 4407)

CChangeInspectView::CChangeInspectView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangeInspectView::~CChangeInspectView(){}

void CChangeInspectView::Init(){
	m_editorDrawer.Clear();
	m_aHousing.clear();
	m_editorDrawer.AddItem(new CHeaderItem(ILanguageManager::GetLanguageManager()->GetString(CHANGE_INSPECT_ID), TITLE_ID));

	CInspectManager* pManager = IJournalComponent::GetInspectManager();
	CInspectManager::journal jrn = pManager->GetJournal(m_nIdParameter);
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_DATE_ID), DATE_INSPECT_ID, editItem::DATE, CConvertFunction::ConvertFromDate(jrn.m_date)));
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FRAME_ID), FRAME_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nFrame)));
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_BEES_ID), STRENGTH_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nStrength)));
	CComboBoxItem* pComboItem = new CComboBoxItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_EBQ_ID), EBQ_INSPECT_ID);
	pComboItem->AddValueItem(Journal::DEFAULT, ILanguageManager::GetLanguageManager()->GetString(QUEEN_BEE_STATUS_DEFAULT_ID));
	pComboItem->AddValueItem(Journal::LOOK, ILanguageManager::GetLanguageManager()->GetString(QUEEN_BEE_STATUS_LOOK_ID));
	pComboItem->AddValueItem(Journal::ABSENCE, ILanguageManager::GetLanguageManager()->GetString(QUEEN_BEE_STATUS_ABSENCE_ID));
	pComboItem->AddValueItem(Journal::FETAL, ILanguageManager::GetLanguageManager()->GetString(QUEEN_BEE_STATUS_FETAL_ID));
	pComboItem->SetValue(jrn.m_nQBState);
	m_editorDrawer.AddItem(pComboItem);
	CRecordViewSettings jSettings;
	CEditItem* pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_WAX_ID), WAX_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nWax));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_GIVE_WOX_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_AMBROSIA_ID), AMBROSIA_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nAmbrosia));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_AMBROSIA_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_PUMP_HONEY_ID), PUMP_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nEvacuation));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_PUMP_HONEY_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FEEDING_ID), FEEDING_INSPECT_ID, editItem::INTEGER, CConvertFunction::ConvertFromInt(jrn.m_nDieting));
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_FEEDING_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	pComboItem = new CComboBoxItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_DISEASES_ID), DISEASES_INSPECT_ID);
	for(int i = 0, id = DISEASE_HEALTHY_ID; i < 33; i++, id++)
		pComboItem->AddValueItem(i, ILanguageManager::GetLanguageManager()->GetString(id));
	pComboItem->SetValue(jrn.m_nDisease);
	m_editorDrawer.AddItem(pComboItem);
	if(jSettings.GetCurrentValue(CHECK_DISEASES_ID) != CHECK_BOX_STATE_1)
		pComboItem->Hide(true);
	pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_INSPECT_ID, editItem::TEXT, jrn.m_strNote);
	m_editorDrawer.AddItem(pItem);
	if(jSettings.GetCurrentValue(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);

	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::INSPECT, USER_FIELD_ID, jrn.m_nIdRecord);

	std::vector<CInspectManager::inspect> aInspects = pManager->GetInspects(m_nIdParameter);
	for(std::vector<CInspectManager::inspect>::iterator it = aInspects.begin(); it != aInspects.end();it++){
		m_editorDrawer.AddItem(new CSectionItem(ILanguageManager::GetLanguageManager()->GetString(HOUSING_ID) + _T(" ") + it->m_strNameHousing, HOUSING_FIELD_ID(it->m_nIdHousing, 0)));
		m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_BROOD_ID), HOUSING_FIELD_ID(it->m_nIdHousing, BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->m_nBrood)));
		m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_HONEY_ID), HOUSING_FIELD_ID(it->m_nIdHousing, HONEY_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->m_nHoney)));
		pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_OPEN_BROOD_ID), HOUSING_FIELD_ID(it->m_nIdHousing, OPEN_BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->m_nOpenBrood));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_OPEN_BROOD_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
		pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_CLOSE_BROOD_ID), HOUSING_FIELD_ID(it->m_nIdHousing, CLOSE_BROOD_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->m_nCloseBrood));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_SEAL_BROOD_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
		pItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_GOODS_HONEY_ID), HOUSING_FIELD_ID(it->m_nIdHousing, GOOGS_HONEY_FIELD_ID), editItem::INTEGER, CConvertFunction::ConvertFromInt(it->m_nGoodHoney));
		m_editorDrawer.AddItem(pItem);
		if(jSettings.GetCurrentValue(CHECK_GOODS_HONEY_ID) != CHECK_BOX_STATE_1)
			pItem->Hide(true);
		m_aHousing.push_back(housing(it->m_nIdHousing, it->m_strNameHousing));
	}
}

void CChangeInspectView::SetState(){
	m_pState->m_nState = Journal::VIEW_CHANGE_INSPECT;
}
bool CChangeInspectView::IsViewState(State state){
	return state.m_type == Journal::CHANGE_INSPECT;
}

BEGIN_MESSAGE_MAP(CChangeInspectView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CChangeInspectView::OnSavePoint(){
	CInspectManager* pManager = IJournalComponent::GetInspectManager();
	CInspectManager::journal jrn;
	jrn.m_date = CConvertFunction::ConvertFromDate(m_editorDrawer.GetValueItem(DATE_INSPECT_ID));
	jrn.m_nFrame = _ttoi(m_editorDrawer.GetValueItem(FRAME_INSPECT_ID).c_str());
	jrn.m_nStrength = _ttoi(m_editorDrawer.GetValueItem(STRENGTH_INSPECT_ID).c_str());
	jrn.m_nQBState = (Journal::queen_bee)_ttoi(m_editorDrawer.GetValueItem(EBQ_INSPECT_ID).c_str());
	jrn.m_nWax = _ttoi(m_editorDrawer.GetValueItem(WAX_INSPECT_ID).c_str());
	jrn.m_nAmbrosia = _ttoi(m_editorDrawer.GetValueItem(AMBROSIA_INSPECT_ID).c_str());
	jrn.m_nEvacuation = _ttoi(m_editorDrawer.GetValueItem(PUMP_INSPECT_ID).c_str());
	jrn.m_nDieting = _ttoi(m_editorDrawer.GetValueItem(FEEDING_INSPECT_ID).c_str());
	jrn.m_nDisease = (Journal::disease)_ttoi(m_editorDrawer.GetValueItem(DISEASES_INSPECT_ID).c_str());
	jrn.m_strNote = m_editorDrawer.GetValueItem(NOTE_INSPECT_ID);
	std::map<std::tstring, std::tstring> aUserFields;
	for(int i = 1; i <= m_nUserFields; i++)
		aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	std::vector<CInspectManager::inspect> aInspects;
	for(std::vector<housing>::iterator it = m_aHousing.begin(); it != m_aHousing.end(); it++){
		CInspectManager::inspect ins;
		ins.m_nIdHousing = it->m_nId;
		ins.m_strNameHousing = it->m_strName;
		ins.m_nBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->m_nId, BROOD_FIELD_ID)).c_str());
		ins.m_nHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->m_nId, HONEY_FIELD_ID)).c_str());
		ins.m_nOpenBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->m_nId, OPEN_BROOD_FIELD_ID)).c_str());
		ins.m_nCloseBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->m_nId, CLOSE_BROOD_FIELD_ID)).c_str());
		ins.m_nGoodHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->m_nId, GOOGS_HONEY_FIELD_ID)).c_str());
		aInspects.push_back(ins);
	}
	pManager->ChangeInspect(m_nIdParameter, jrn, aInspects, aUserFields);
	__super::OnCancel();
}